import { createRouter, createWebHashHistory, RouteLocation, RouteLocationRaw, RouteRecord } from 'vue-router';
import type { RouteRecordRaw } from 'vue-router';
import Layout from '@/layout/index.vue';
import { Utils } from '@/common/utils';

// 路由类型
export declare type RouteRecordType = {
    hidden?: string | boolean;
    meta?: {
        title?: string;
        type?: 'home';
        icon?: string;
        permission?: string[];
        active?: string
        isLogin?: boolean //是否需要登录
    };
    children?: RouteRecordType[];
} & RouteRecordRaw

/**
 * 处理重定向
 * @param to 当前匹配路径
 * @param path 匹配对应路由
 */
function setRedirect(to: RouteLocation, path: string): RouteLocationRaw {
    let route_name: any;
    to.matched.forEach((item: RouteRecord) => {
        if (item.path === path) {
            const router = item.children.filter((item: RouteRecordType) => !item?.hidden);
            route_name = router[0].name;
        }
    });
    return { name: route_name }
}

// 静态路由
export const constantRouters: Array<RouteRecordType> = [
    {
        path: '/',
        meta: {},
        component: Layout as any,
        redirect: (to: RouteLocation) => setRedirect(to, '/'),
        children: [
            {
                path: 'index',
                name: 'home',
                meta: {},
                component: () => Utils.getSystem().is_mobile ? import('@/views/home/mobile_index.vue') : import('@/views/home/index.vue')
            }
        ]
    },
    {
        path: '/largeScreen',
        name: 'largeScreen',
        meta: {},
        component: () => import('@/views/three/index.vue')
    },
    {
        path: '/adminLogin',
        name: 'adminLogin',
        meta: {},
        component: () => import('@/views/login/adminLogin.vue')
    },
    {
        path: '/userLogin',
        meta: {},
        component: Layout,
        redirect: (to: RouteLocation) => setRedirect(to, '/'),
        children: [
            {
                path: 'index',
                name: 'login',
                meta: {},
                component: () => Utils.getSystem().is_mobile ? import('@/views/home/mobile_index.vue') : import('@/views/login/index.vue')
            }
        ]
    },
    {
        path: '/test',
        name: 'test',
        meta: {},
        component: import('@/views/test/index.vue')
    },
    {
        path: '/clothing',
        meta: {},
        component: Layout,
        redirect: (to: RouteLocation) => setRedirect(to, '/clothing'),
        children: [
            {
                path: 'index',
                name: 'clothing',
                meta: {},
                // component: () => Utils.getSystem().is_mobile ? import('@/views/clothing/mobile_index.vue') : import('@/views/clothing/index.vue')
                component: () => import('@/views/clothing/index.vue')
            }
        ]
    },
    {
        path: '/news',
        meta: {},
        component: Layout,
        redirect: (to: RouteLocation) => setRedirect(to, '/news'),
        children: [
            {
                path: 'index',
                name: 'news',
                meta: {},
                component: () => import('@/views/news/index.vue')
            },
            {
                path: 'details',
                name: 'newsDetails',
                meta: {},
                component: () => import('@/views/news/details.vue')
            },
        ]
    },
    {
        path: '/shop',
        meta: {},
        component: Layout,
        redirect: (to: RouteLocation) => setRedirect(to, '/shop'),
        children: [
            {
                path: 'index',
                name: 'shop',
                meta: {},
                component: () => import('@/views/shop/index.vue')
            },
            {
                path: 'details',
                name: 'shopDetails',
                meta: {},
                component: () => import('@/views/shop/details.vue')
            },
        ]
    },
    {
        path: '/recruit',
        meta: {},
        component: Layout,
        redirect: (to: RouteLocation) => setRedirect(to, '/recruit'),
        children: [
            {
                path: 'index',
                name: 'recruit',
                meta: {},
                component: () => import('@/views/recruit/index.vue')
            },
        ]
    },
    {
        path: '/personalCenter',
        meta: {},
        component: Layout,
        redirect: (to: RouteLocation) => setRedirect(to, '/personalCenter'),
        children: [
            {
                path: 'index',
                name: 'personalCenter',
                meta: {},
                component: () => import('@/views/personalCenter/index.vue')
            },
            {
                path: 'personalData',
                name: 'personalData',
                meta: {},
                component: () => import('@/views/personalCenter/personalData.vue')
            },
        ]
    },
    {
        path: '/business',
        meta: {},
        component: Layout,
        redirect: (to: RouteLocation) => setRedirect(to, '/business'),
        children: [
            {
                path: 'index',
                name: 'business',
                meta: {},
                component: () => import('@/views/business/index.vue')
            }
        ]
    },
    {
        path: '/brand',
        meta: {},
        component: Layout,
        redirect: (to: RouteLocation) => setRedirect(to, '/brand'),
        children: [
            {
                path: 'index',
                name: 'brand',
                meta: {},
                component: () => import('@/views/brand/index.vue')
            }
        ]
    },
    {
        path: '/about',
        meta: {},
        component: Layout,
        redirect: (to: RouteLocation) => setRedirect(to, '/about'),
        children: [
            {
                path: 'index',
                name: 'about',
                meta: {},
                component: () => import('@/views/about/index.vue')
            }
        ]
    },
]

const router = createRouter({
    history: createWebHashHistory(),
    routes: constantRouters,
    scrollBehavior() {
        return { top: 0 }
    }
});

export default router;